<?php
/**
 * 定时任务
 * @author Administrator
 *
 */
class Crontab extends Controller{
    var $user = null;
    function Crontab(){
        parent::Controller();
        $this->user = array(
            'userId'    => 0,//用户ID
            'userName'  => '',//用户名称
            'userRole'  => 0,//用户角色
            'userLimit' => array(),//用户权限
        );
        //设置超时
        set_time_limit(20);
    }
    /**
    * 执行定时任务入口
    */
    function index(){
        //设置超时时间
        set_time_limit(0);
        echo "<font color='red'>执行定时任务开始</font><br>";
        //$this->xunSync();
        //单店项目预算自动审核
        $this->budgetSingleProjectAutoAudit();
        //车展数据同步
        $this->carSyncCmd();
        echo "<font color='red'>执行定时任务结束</font><br>";
    }
    /**
     * 单店项目预算自动审核
     */
    function budgetSingleProjectAutoAudit(){
        $this->load->model('singleProjectModel');
        $this->load->model('singlePGxModel');
        $this->load->model('singlePOfflineModel');
        //项目ID集
        $projectIds = array();
        $time = strtotime('-1 day');
        echo "<font color='red'>开始项目预算自动审核</font><br>";
        //广宣ID集
        $gxList = $this->singlePGxModel->getList(array('budgetAuditRs'=>0,'budgetSubTime < '=>$time), NULL, NULL, 'id,projectId,budgetSubTime');
        foreach ($gxList as $v){
            $projectIds[(int)$v['projectId']]['gx'][] = (int)$v['id'];
        }
        //线下ID集
        $offlineList = $this->singlePOfflineModel->getList(array('budgetAuditRs'=>0,'budgetSubTime < '=>$time), NULL, NULL, 'id,projectId,budgetSubTime');
        foreach ($offlineList as $v){
            $projectIds[(int)$v['projectId']]['offline'][] = (int)$v['id'];
        }
        if ($projectIds){
            $projectList = $this->singleProjectModel->getList(array('id'=>array('in', array_keys($projectIds) )));
            foreach ($projectList as $v){
                if ($v['fixedMoney'] >= $v['budgetApply']){//不超过定额,一天内自动审核
                    if ($time - $v['budgetSubTime'] > 86400){
                        if (isset($projectIds[$v['id']]['gx'])){
                            foreach ($projectIds[$v['id']]['gx'] as $gxv){
                                $this->singlePGxModel->budgetAudit($gxv, 1, $this->user);
                            }
                        }
                        if (isset($projectIds[$v['id']]['offline'])){
                            foreach ($projectIds[$v['id']]['offline'] as $offlinev){
                                $this->singlePOfflineModel->budgetAudit($offlinev, 1, $this->user);
                            }
                        }
                        $this->singleProjectModel->budgetUpdateStatus($v['id']);
                        $this->singleProjectModel->updateMoney($v['id']);
                        echo "项目方案编号为【".$v['id']."】预算自动审核成功<br> \n";
                        usleep(500);
                    }
                }else{//超过定额，七天内审核
                    if ($time - $v['budgetSubTime'] > 86400*5){
                        if (isset($projectIds[$v['id']]['gx'])){
                            foreach ($projectIds[$v['id']]['gx'] as $gxv){
                                $this->singlePGxModel->budgetAudit($gxv, 1, $this->user);
                            }
                        }
                        if (isset($projectIds[$v['id']]['offline'])){
                            foreach ($projectIds[$v['id']]['offline'] as $offlinev){
                                $this->singlePOfflineModel->budgetAudit($offlinev, 1, $this->user);
                            }
                        }
                        $this->singleProjectModel->budgetUpdateStatus($v['id']);
                        $this->singleProjectModel->updateMoney($v['id']);
                        echo "项目方案编号为【".$v['id']."】预算自动审核成功<br> \n";
                        usleep(500);
                    }
                }
                //暂停500微妙
                echo str_repeat(" ",4096);                
            }
        }
        echo "<font color='red'>结束项目预算自动审核</font><br>";
    }
    /**
     * 车展预算自动审核
     */
    function budgetSingleCarshowAutoAudit(){
        $this->load->model('sCarshowModel');
        $this->load->model('sCGxModel');
        $this->load->model('sCRentModel');
        $this->load->model('sCMatchingModel');
        //项目ID集
        $projectIds = array();
        $time = strtotime('-7 day');
        echo "<font color='red'>开始非城市地方车展预算自动审核</font><br>";
        //广宣ID集
        $gxList = $this->sCGxModel->getList(array('budgetAuditRs'=>0,'budgetSubTime < '=>$time));
        foreach ($gxList as $v){
            $projectIds[(int)$v['carshowId']]['gx'][] = (int)$v['id'];
        }
        //场租ID集
        $rentList = $this->sCRentModel->getList(array('budgetAuditRs'=>0,'budgetSubTime < '=>$time));
        foreach ($rentList as $v){
            $projectIds[(int)$v['carshowId']]['rent'][] = (int)$v['id'];
        }
        //车展配套ID集
        $matchingList = $this->sCMatchingModel->getList(array('budgetAuditRs'=>0,'budgetSubTime < '=>$time));
        foreach ($matchingList as $v){
            $projectIds[(int)$v['carshowId']]['matching'][] = (int)$v['id'];
        }
        if ($projectIds){
            $carshowList = $this->sCarshowModel->getList(array('id'=>array('in', array_keys($projectIds))) );
            foreach ($carshowList as $v){
                if (isset($projectIds[$v['id']]['gx'])){
                    foreach ($projectIds[$v['id']]['gx'] as $gxv){
                        $this->sCGxModel->budgetAudit($gxv, 1, $this->user);
                    }
                }
                if (isset($projectIds[$v['id']]['rent'])){
                    foreach ($projectIds[$v['id']]['rent'] as $rentv){
                        $this->sCRentModel->budgetAudit($rentv, 1, $this->user);
                    }
                }
                if (isset($projectIds[$v['id']]['matching'])){
                    foreach ($projectIds[$v['id']]['matching'] as $matchingv){
                        $this->sCMatchingModel->budgetAudit($matchingv, 1, $this->user);
                    }
                }
                $this->sCarshowModel->budgetUpdateStatus($v['id']);
                $this->sCarshowModel->updateMoney($v['id']);
                
                echo "地方车展编号为【".$v['id']."】预算自动审核成功<br> \n";
                //暂停500微妙
                echo str_repeat(" ",4096);
                usleep(500);
            }
        }
        echo "<font color='red'>结束非城市地方车展预算自动审核</font><br>";
    }
    /**
     * 增强类RMP专项预算自动审核
     */
    function budgetSingleRmpAutoAudit(){
        $this->load->model('specialProjectModel');
        $this->load->model('specialPGxModel');
        $this->load->model('specialPOfflineModel');
        //项目ID集
        $projectIds = array();
        $time = strtotime('-7 day');
        echo "<font color='red'>开始增强类RMP专项预算自动审核</font><br>";
        //广宣ID集
        $gxList = $this->specialPGxModel->getList(array('budgetAuditRs'=>0,'budgetSubTime < '=>$time));
        foreach ($gxList as $v){
            $projectIds[(int)$v['projectId']]['gx'][] = (int)$v['id'];
        }
        //线下ID集
        $offlineList = $this->specialPOfflineModel->getList(array('budgetAuditRs'=>0,'budgetSubTime < '=>$time));
        foreach ($offlineList as $v){
            $projectIds[(int)$v['projectId']]['offline'][] = (int)$v['id'];
        }
        if ($projectIds){
            $projectList = $this->specialProjectModel->getList(array('id'=>array('in', array_keys($projectIds) )));
            foreach ($projectList as $v){
                if (isset($projectIds[$v['id']]['gx'])){
                    foreach ($projectIds[$v['id']]['gx'] as $gxv){
                        $this->specialPGxModel->budgetAudit($gxv, 1, $this->user);
                    }
                }
                if (isset($projectIds[$v['id']]['offline'])){
                    foreach ($projectIds[$v['id']]['offline'] as $offlinev){
                        $this->specialPOfflineModel->budgetAudit($offlinev, 1, $this->user);
                    }
                }
                $this->specialProjectModel->budgetUpdateStatus($v['id']);
                $this->specialProjectModel->updateMoney($v['id']);
                echo "增强类RMP专项方案编号为【".$v['id']."】预算自动审核成功<br> \n";
                usleep(500);
                //暂停500微妙
                echo str_repeat(" ",4096);
            }
        }
        echo "<font color='red'>结束增强类RMP专项预算自动审核</font><br>";
    }
    /**
     * 巡展同步
     */
    function xunSync(){
        //error_reporting(E_ALL);
        //ini_set('display_errors', 1);
        //设置超时时间
        set_time_limit(0);
        $year = 2016;//当前年份
        $nowMonth = date("m");//当前月份
        $updateMax = 10;//每次同步数量
        $state = isset($_GET['state']) ? (int)$_GET['state'] : 0;//获取标识（0：待同步，1：已同步）
        //加载巡展同步类
        $this->load->library('XunSync', NULL, 'XunSync');
        for ($month = 1; $month <= $nowMonth; $month++){
            echo '【更新'.$month.'月份数据开始】<br>';
            //项目预算同步
            $beginNumber = 1;
            do{
                $rs = $this->XunSync->budgetProjectSync($year, $month, $updateMax, $beginNumber, '08', $state);
                $codeStr = $rs['code']==1?'<font color="green">成功</font>':'<font color="red">失败</font>';
                $log = '更新项目预算情况（[年份：'.$year.'],[月份:'.$month.'],[更新状态：'.$codeStr.'],[更新反馈：'.$rs['msg'].'],[更新数量：'.(isset($rs['data']['updateCount']) ? $rs['data']['updateCount'] : 0).']）<br>';
                echo $log;
                //记录错误日志
                if ($rs['code'] == 0){
                    $this->xunSyncRecord('项目预算同步错误', $log);
                }
                echo str_repeat(" ",4096);
                //usleep(500);
                $updateCount = isset($rs['data']['total']) ? $rs['data']['total'] : 0;
                $state == 1 && $beginNumber ++;
            }while ( $updateCount == $updateMax);
            //广宣预算同步
            $beginNumber = 1;
            do{
                $rs = $this->XunSync->budgetGxSync($year, $month, $updateMax, $beginNumber, '08', $state);
                $codeStr = $rs['code']==1?'<font color="green">成功</font>':'<font color="red">失败</font>';
                $log = '更新广宣预算情况（[年份：'.$year.'],[月份:'.$month.'],[更新状态：'.$codeStr.'],[更新反馈：'.$rs['msg'].'],[更新数量：'.(isset($rs['data']['updateCount']) ? $rs['data']['updateCount'] : 0).']）<br>';
                echo $log;
                //记录错误日志
                if ($rs['code'] == 0){
                    $this->xunSyncRecord('广宣预算同步错误', $log);
                }
                echo str_repeat(" ",4096);
                //usleep(500);
                $updateCount = isset($rs['data']['total']) ? $rs['data']['total'] : 0;
                $state == 1 && $beginNumber ++;
            }while ( $updateCount == $updateMax);
            //线下活动预算同步
            $beginNumber = 1;
            do{
                $rs = $this->XunSync->budgetOfflineSync($year, $month, $updateMax, $beginNumber, '08', $state);
                $codeStr = $rs['code']==1?'<font color="green">成功</font>':'<font color="red">失败</font>';
                $log ='更新线下活动预算情况（[年份：'.$year.'],[月份:'.$month.'],[更新状态：'.$codeStr.'],[更新反馈：'.$rs['msg'].'],[更新数量：'.(isset($rs['data']['updateCount']) ? $rs['data']['updateCount'] : 0).']）<br>';
                echo $log;
                //记录错误日志
                if ($rs['code'] == 0){
                    $this->xunSyncRecord('线下活动预算同步错误', $log);
                }
                echo str_repeat(" ",4096);
                //usleep(500);
                $updateCount = isset($rs['data']['total']) ? $rs['data']['total'] : 0;
                $state == 1 && $beginNumber ++;
            }while ( $updateCount == $updateMax);
            //项目结算同步
            $beginNumber = 1;
            do{
                $rs = $this->XunSync->settleProjectSync($year, $month, $updateMax, $beginNumber, '08', $state);
                $codeStr = $rs['code']==1?'<font color="green">成功</font>':'<font color="red">失败</font>';
                $log ='更新项目结算情况（[年份：'.$year.'],[月份:'.$month.'],[更新状态：'.$codeStr.'],[更新反馈：'.$rs['msg'].'],[更新数量：'.(isset($rs['data']['updateCount']) ? $rs['data']['updateCount'] : 0).']）<br>';
                echo $log;
                //记录错误日志
                if ($rs['code'] == 0){
                    $this->xunSyncRecord('项目结算同步错误', $log);
                }
                echo str_repeat(" ",4096);
                //usleep(500);
                $updateCount = isset($rs['data']['total']) ? $rs['data']['total'] : 0;
                $state == 1 && $beginNumber ++;
            }while ( $updateCount == $updateMax);
            //广宣结算同步
            $beginNumber = 1;
            do{
                $rs = $this->XunSync->settleGxSync($year, $month, $updateMax, $beginNumber, '08', $state);
                $codeStr = $rs['code']==1?'<font color="green">成功</font>':'<font color="red">失败</font>';
                $log ='更新广宣结算情况（[年份：'.$year.'],[月份:'.$month.'],[更新状态：'.$codeStr.'],[更新反馈：'.$rs['msg'].'],[更新数量：'.(isset($rs['data']['updateCount']) ? $rs['data']['updateCount'] : 0).']）<br>';
                echo $log;
                //记录错误日志
                if ($rs['code'] == 0){
                    $this->xunSyncRecord('广宣结算同步错误', $log);
                }
                echo str_repeat(" ",4096);
                //usleep(500);
                $updateCount = isset($rs['data']['total']) ? $rs['data']['total'] : 0;
                $state == 1 && $beginNumber ++;
            }while ( $updateCount == $updateMax);
            //线下活动结算同步
            $beginNumber = 1;
            do{
                $rs = $this->XunSync->settleOfflineSync($year, $month, $updateMax, $beginNumber, '08', $state);
                $codeStr = $rs['code']==1?'<font color="green">成功</font>':'<font color="red">失败</font>';
                $log ='更新线下活动结算情况（[年份：'.$year.'],[月份:'.$month.'],[更新状态：'.$codeStr.'],[更新反馈：'.$rs['msg'].'],[更新数量：'.(isset($rs['data']['updateCount']) ? $rs['data']['updateCount'] : 0).']）<br>';
                echo $log;
                //记录错误日志
                if ($rs['code'] == 0){
                    $this->xunSyncRecord('线下活动结算同步错误', $log);
                }
                echo str_repeat(" ",4096);
                usleep(500);
                $updateCount = isset($rs['data']['total']) ? $rs['data']['total'] : 0;
                $state == 1 && $beginNumber ++;
            }while ( $updateCount == $updateMax);
            echo '【更新'.$month.'月份数据结束】<br>';
        }
    }
    /**
     * 记录巡展同步日志
     * @param string $typeName
     * @param string $log
     */
    function xunSyncRecord($typeName, $log){
        $this->load->model('xLogModel');
        $this->xLogModel->add(array('typeName'=>$typeName,'log'=>$log,'createTime'=>date("Y-m-d H:i:s")));
        //邮件提醒
        $this->load->library('email');
        //发件人
        $this->email->from('zhaojianhui129@163.com', '巡展同步邮件警报');
        //收件人
        $this->email->to('zhaojianhui129@vip.qq.com');
        //抄送
        $this->email->cc(array('ivy.huang@bluedoorindex.com'));
        //暗送
        //$this->email->bcc('them@their-example.com');
        
        $this->email->subject($typeName);
        $this->email->message($log);
        
        $this->email->send();
        //echo $this->email->print_debugger();
    }
    /**
     * 删除无效的巡展
     * @return [type] [description]
     */
    function xunDel(){
        //设置超时时间
        set_time_limit(0);
        $this->load->model('xProjectModel');
        $this->load->model('xPGxModel');
        $this->load->model('xPOfflineModel');
        $this->load->model('xPODetailModel');
        $delList = $this->xProjectModel->getList(array('siteSpecs !='=>1));
        foreach ($delList as $k => $v) {
            $gxList = $this->xPGxModel->getList(array('activityId'=>$v['activityId']));
            foreach ($gxList as $gxK => $gxV) {
                $this->xPGxModel->del(array('costNo'=>$gxV['costNo']), '*');
            }
            $offlineList = $this->xPOfflineModel->getList(array('activityId'=>$v['activityId']));
            foreach ($offlineList as $offK => $offV) {
                $this->xPODetailModel->del(array('offlineCostNo'=>$offV['costNo']), '*');
                $this->xPOfflineModel->del(array('costNo'=>$offV['costNo']), '*');
            }
            $this->xProjectModel->del(array('activityId'=>$v['activityId']), '*');
            echo "删除项目".$v['activityId']."成功<br>";
            echo str_repeat(" ",4096);
            usleep(500);
        }
    }
    /**
     * 更新单店基金汇总金额
     */
    function updateSingleProcess(){
        $storeId = (int)$this->input->get('storeId');
        $storeId || exit('请选择专营店');
        $timeUnit = (int)$this->input->get('timeUnit');
        $timeUnit || exit('请选择时段');
        $year = (int)$this->input->get('year');
        $year || exit('请选择年份');
        $this->load->model('singleProcessModel');
        $findData = $this->singleProcessModel->getData(array('storeId'=>$storeId,'timeUnit'=>$timeUnit,'year'=>$year));
        if ($findData){
            $this->singleProcessModel->updateMoney((int)$findData['id']);
            echo "更新成功";
        }else{
            echo "没有对应双月数据";
        }
    }
    /**
     * 车展数据同步
     */
    function carSyncCmd(){
        $this->load->library('CarSync', NULL, 'CarSync');
        //同步汇总数据
        $this->carSumSync();
        sleep(1);
        //同步项目数据
        $this->carProjectSync();
        sleep(1);
        //同步广宣数据
        $this->carGxSync();
        sleep(1);
        //同步场租数据
        $this->carRentSync();
        sleep(1);
        //同步配套数据
        $this->carMatchingSync();
    }
    /**
     * 车展汇总金额数据提交
     */
    function carSumSync(){
        $this->load->model('carProcessModel');
        $this->load->model('sellpointModel');
        $this->load->model('fileModel');
        //设置超时时间
        set_time_limit(0);
        $year = 2016;//当前年份
        $updateMax = 10;//每次同步数量
        $where = array('isSync'=>0);
        //$where = array('isSync'=>0,'id'=>array('in',array(16,107,109,111)));
        $count = $this->carProcessModel->getCount($where);
        //执行次数（最多执行20步）
        $step = 0;
        do{
            $storeIds = array();
            $fileIds = array();
            $list = $this->carProcessModel->getNewList($where, $updateMax, 0, '', 'id desc');
            foreach ($list as $v){
                in_array((int) $v['storeId'], $storeIds) || $storeIds[] = (int) $v['storeId'];
                in_array((int) $v['fileId'], $fileIds) || $fileIds[] = (int) $v['fileId'];
            }
            $storeList = $this->sellpointModel->getStoreListByIds($storeIds);
            $fileList = $this->fileModel->getList(array('id'=>array('in',$fileIds)));
            foreach ($list as $k => $v){
                //专营店信息
                $storeData = isset($storeList[(int)$v['storeId']]) ? $storeList[(int)$v['storeId']] : array();
                $v['dlrCode'] = isset($storeData['coding']) ? $storeData['coding'] : '';
                $v['dlrName'] = isset($storeData['storeName']) ? $storeData['storeName'] : '';
                //附件信息
                $fileData = isset($fileList[(int) $v['fileId']]) ? $fileList[(int) $v['fileId']] : array();
                $v['fileUrl'] = $fileData ? 'http://www.dflpvmkt.com.cn'.$fileData['viewPath']: '';
                
                $list[$k] = $v;
            }
            
            $res = $this->CarSync->packSumData($list);
            //同步成功则更改同步状态
            if ($res){
                $this->carProcessModel->edit(array('id'=>array('in',array_keys($list))), array('isSync'=>1),'*');
                echo "<font color='green'>同步汇总数据成功</font><br>";
            }else{
                echo "<font color='red'>同步汇总数据失败</font><br>";
            }
            $count = $this->carProcessModel->getCount($where);
            $step ++;
            echo str_repeat(" ",4096);
        }while ( $count > 0 && $step < 100);
    }
    /**
     * 车展项目数据提交
     */
    function carProjectSync(){
        $this->load->model('carProcessModel');
        $this->load->model('carProjectModel');
        $this->load->model('sellpointModel');
        //设置超时时间
        set_time_limit(0);
        $year = 2016;//当前年份
        $updateMax = 10;//每次同步数量
        $where = array('isSync'=>0,'settleAuditRs >'=>-1);
        //$where = array('isSync'=>0,'procId'=>array('in',array(16,107,109,111)));         
        $count = $this->carProjectModel->getCount($where);
        //执行次数（最多执行20步）
        $step = 0;
        do{
            $procIds = array();
            $storeIds = array();
            $list = $this->carProjectModel->getNewList($where, $updateMax, 0, '', 'id desc');
            foreach ($list as $v){
                in_array((int) $v['storeId'], $storeIds) || $storeIds[] = (int) $v['storeId'];
                in_array((int) $v['procId'], $procIds) || $procIds[] = (int) $v['procId'];
            }
            $storeList = $this->sellpointModel->getStoreListByIds($storeIds);
            $procList = $this->carProcessModel->getNewList(array('id'=>array('in',$procIds)));
            foreach ($list as $k => $v){
                //专营店信息
                $storeData = isset($storeList[(int)$v['storeId']]) ? $storeList[(int)$v['storeId']] : array();
                $v['dlrCode'] = isset($storeData['coding']) ? $storeData['coding'] : '';
                $v['dlrName'] = isset($storeData['storeName']) ? $storeData['storeName'] : '';
                //汇总数据
                $procData = isset($procList[(int)$v['procId']]) ? $procList[(int)$v['procId']] : array();
                $v['year'] = isset($procData['year']) ? $procData['year'] : '';
                $v['timeUnitStr'] = isset($procData['timeUnitStr']) ? $procData['timeUnitStr'] : '';
                
                $list[$k] = $v;
            }
            $res = $this->CarSync->packProjectData($list);
            //同步成功则更改同步状态
            if ($res){
                $this->carProjectModel->edit(array('id'=>array('in',array_keys($list))), array('isSync'=>1),'*');
                echo "<font color='green'>同步项目数据成功</font><br>";
            }else{
                echo "<font color='red'>同步项目数据失败</font><br>";
            }
            $count = $this->carProjectModel->getCount($where);
            $step ++;
            echo str_repeat(" ",4096);
        }while ( $count > 0 && $step < 100);
    }
    /**
     * 车展广宣数据提交
     */
    function carGxSync(){
        $this->load->model('carProcessModel');
        $this->load->model('carGxModel');
        $this->load->model('sellpointModel');
        $this->load->model('fileModel');
        //设置超时时间
        set_time_limit(0);
        $year = 2016;//当前年份
        $updateMax = 10;//每次同步数量
        $where = array('isSync'=>0,'settleAuditRs >'=>-1);
        //$where = array('isSync'=>0,'procId'=>array('in',array(16,107,109,111)));
        $count = $this->carGxModel->getCount($where);
        //执行次数（最多执行20步）
        $step = 0;
        do{
            $procIds = array();
            $storeIds = array();
            $fileIds = array();
            $list = $this->carGxModel->getNewList($where, $updateMax, 0, '', 'id desc');
            foreach ($list as $v){
                in_array((int) $v['storeId'], $storeIds) || $storeIds[] = (int) $v['storeId'];
                in_array((int) $v['procId'], $procIds) || $procIds[] = (int) $v['procId'];
                in_array((int) $v['fileId'], $fileIds) || $fileIds[] = (int) $v['fileId'];
            }
            $storeList = $this->sellpointModel->getStoreListByIds($storeIds);
            $procList = $this->carProcessModel->getNewList(array('id'=>array('in',$procIds)));
            $fileList = $this->fileModel->getList(array('id'=>array('in',$fileIds)));
            foreach ($list as $k => $v){
                //专营店信息
                $storeData = isset($storeList[(int)$v['storeId']]) ? $storeList[(int)$v['storeId']] : array();
                $v['dlrCode'] = isset($storeData['coding']) ? $storeData['coding'] : '';
                $v['dlrName'] = isset($storeData['storeName']) ? $storeData['storeName'] : '';
                //汇总数据
                $procData = isset($procList[(int)$v['procId']]) ? $procList[(int)$v['procId']] : array();
                $v['year'] = isset($procData['year']) ? $procData['year'] : '';
                $v['timeUnitStr'] = isset($procData['timeUnitStr']) ? $procData['timeUnitStr'] : '';
                //附件信息
                $fileData = isset($fileList[(int) $v['fileId']]) ? $fileList[(int) $v['fileId']] : array();
                $v['fileUrl'] = $fileData ? 'http://www.dflpvmkt.com.cn'.$fileData['viewPath']: '';
                
                $list[$k] = $v;
            }
            $res = $this->CarSync->packGxData($list);
            //同步成功则更改同步状态
            if ($res){
                $this->carGxModel->edit(array('id'=>array('in',array_keys($list))), array('isSync'=>1),'*');
                echo "<font color='green'>同步广宣数据成功</font><br>";
            }else{
                echo "<font color='red'>同步广宣数据失败</font><br>";
            }
            $count = $this->carGxModel->getCount($where);
            $step ++;
            echo str_repeat(" ",4096);
        }while ( $count > 0 && $step < 100);
    }
    /**
     * 车展基金场租数据提交
     */
    function carRentSync(){
        $this->load->model('carProcessModel');
        $this->load->model('carRentModel');
        $this->load->model('sellpointModel');
        $this->load->model('fileModel');
        //设置超时时间
        set_time_limit(0);
        $year = 2016;//当前年份
        $updateMax = 10;//每次同步数量
        $where = array('isSync'=>0,'settleAuditRs >'=>-1);
        //$where = array('isSync'=>0,'procId'=>array('in',array(16,107,109,111)));
        $count = $this->carRentModel->getCount($where);
        //执行次数（最多执行20步）
        $step = 0;
        do{
            $procIds = array();
            $storeIds = array();
            $fileIds = array();
            $list = $this->carRentModel->getNewList($where, $updateMax, 0, '', 'id desc');
            foreach ($list as $v){
                in_array((int) $v['storeId'], $storeIds) || $storeIds[] = (int) $v['storeId'];
                in_array((int) $v['procId'], $procIds) || $procIds[] = (int) $v['procId'];
                in_array((int) $v['fileId'], $fileIds) || $fileIds[] = (int) $v['fileId'];
            }
            $storeList = $this->sellpointModel->getStoreListByIds($storeIds);
            $procList = $this->carProcessModel->getNewList(array('id'=>array('in',$procIds)));
            $fileList = $this->fileModel->getList(array('id'=>array('in',$fileIds)));
            foreach ($list as $k => $v){
                //专营店信息
                $storeData = isset($storeList[(int)$v['storeId']]) ? $storeList[(int)$v['storeId']] : array();
                $v['dlrCode'] = isset($storeData['coding']) ? $storeData['coding'] : '';
                $v['dlrName'] = isset($storeData['storeName']) ? $storeData['storeName'] : '';
                //汇总数据
                $procData = isset($procList[(int)$v['procId']]) ? $procList[(int)$v['procId']] : array();
                $v['year'] = isset($procData['year']) ? $procData['year'] : '';
                $v['timeUnitStr'] = isset($procData['timeUnitStr']) ? $procData['timeUnitStr'] : '';
                //附件信息
                $fileData = isset($fileList[(int) $v['fileId']]) ? $fileList[(int) $v['fileId']] : array();
                $v['fileUrl'] = $fileData ? 'http://www.dflpvmkt.com.cn'.$fileData['viewPath']: '';
    
                $list[$k] = $v;
            }
            $res = $this->CarSync->packRentData($list);
            //同步成功则更改同步状态
            if ($res){
                $this->carRentModel->edit(array('id'=>array('in',array_keys($list))), array('isSync'=>1),'*');
                echo "<font color='green'>同步场租数据成功</font><br>";
            }else{
                echo "<font color='red'>同步场租数据失败</font><br>";
            }
            $count = $this->carRentModel->getCount($where);
            $step ++;
            echo str_repeat(" ",4096);
        }while ( $count > 0 && $step < 100);
    }
    /**
     * 车展基金配套数据提交
     */
    function carMatchingSync(){
        $this->load->model('carProcessModel');
        $this->load->model('carMatchingModel');
        $this->load->model('sellpointModel');
        $this->load->model('fileModel');
        //设置超时时间
        set_time_limit(0);
        $year = 2016;//当前年份
        $updateMax = 10;//每次同步数量
        $where = array('isSync'=>0,'settleAuditRs >'=>-1);
        //$where = array('isSync'=>0,'procId'=>array('in',array(16,107,109,111)));
        $count = $this->carMatchingModel->getCount($where);
        //执行次数（最多执行20步）
        $step = 0;
        do{
            $procIds = array();
            $storeIds = array();
            $fileIds = array();
            $list = $this->carMatchingModel->getNewList($where, $updateMax, 0, '', 'id desc');
            foreach ($list as $v){
                in_array((int) $v['storeId'], $storeIds) || $storeIds[] = (int) $v['storeId'];
                in_array((int) $v['procId'], $procIds) || $procIds[] = (int) $v['procId'];
                in_array((int) $v['fileId'], $fileIds) || $fileIds[] = (int) $v['fileId'];
            }
            $storeList = $this->sellpointModel->getStoreListByIds($storeIds);
            $procList = $this->carProcessModel->getNewList(array('id'=>array('in',$procIds)));
            $fileList = $this->fileModel->getList(array('id'=>array('in',$fileIds)));
            foreach ($list as $k => $v){
                //专营店信息
                $storeData = isset($storeList[(int)$v['storeId']]) ? $storeList[(int)$v['storeId']] : array();
                $v['dlrCode'] = isset($storeData['coding']) ? $storeData['coding'] : '';
                $v['dlrName'] = isset($storeData['storeName']) ? $storeData['storeName'] : '';
                //汇总数据
                $procData = isset($procList[(int)$v['procId']]) ? $procList[(int)$v['procId']] : array();
                $v['year'] = isset($procData['year']) ? $procData['year'] : '';
                $v['timeUnitStr'] = isset($procData['timeUnitStr']) ? $procData['timeUnitStr'] : '';
                //附件信息
                $fileData = isset($fileList[(int) $v['fileId']]) ? $fileList[(int) $v['fileId']] : array();
                $v['fileUrl'] = $fileData ? 'http://www.dflpvmkt.com.cn'.$fileData['viewPath']: '';
    
                $list[$k] = $v;
            }
            $res = $this->CarSync->packMatchingData($list);
            //同步成功则更改同步状态
            if ($res){
                $this->carMatchingModel->edit(array('id'=>array('in',array_keys($list))), array('isSync'=>1),'*');
                echo "<font color='green'>同步配套数据成功</font><br>";
            }else{
                echo "<font color='red'>同步配套数据失败</font><br>";
            }
            $count = $this->carMatchingModel->getCount($where);
            $step ++;
            echo str_repeat(" ",4096);
        }while ( $count > 0 && $step < 100);
    }
}